#include "head.h"


int main(int argc, char *argv[]){                                  

    ARGS_CHECK(argc, 4);
    
    // 创建监听socket
    int sockfd = establishConnect(argv[1], argv[2]);
    // 创建线程池
    threadPoolT pool;
    makeThreadPool(&pool, atoi(argv[3]));

    int efd = epoll_create(1);
    epollAdd(efd, sockfd);

    struct epoll_event readySet[1024];

    while(1){
        int readyNum = epoll_wait(efd, readySet, 1024, -1);
        for(int i = 0; i < readyNum; i++){
            int curfd = readySet[i].data.fd;
            
            // 监听到连接
            if(curfd == sockfd){
                // 从全连接队列中取出一条连接建立通信socket
                int connectfd = accept(sockfd, NULL, NULL);
                
               // 将通信socket放入任务队列中
                pthread_mutex_lock(&pool.mutex);
                enQueue(pool.workQueue, connectfd);
                pthread_cond_broadcast(&pool.cond);
                pthread_mutex_unlock(&pool.mutex);
            }
        }
    }



    close(sockfd);
    return 0;
}

